package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralControllerPoint;
import com.ruoyi.central.service.ITopCentralControllerPointService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 地图站点信息Controller
 * 
 * @author ruoyi
 * @date 2022-02-25
 */
@Controller
@RequestMapping("/central/centralControllerPoint")
public class TopCentralControllerPointController extends BaseController
{
    private String prefix = "central/centralControllerPoint";

    @Autowired
    private ITopCentralControllerPointService topCentralControllerPointService;

    @RequiresPermissions("central:centralControllerPoint:view")
    @GetMapping()
    public String centralControllerPoint()
    {
        return prefix + "/centralControllerPoint";
    }

    /**
     * 查询地图站点信息列表
     */
    @RequiresPermissions("central:centralControllerPoint:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralControllerPoint topCentralControllerPoint)
    {
        startPage();
        List<TopCentralControllerPoint> list = topCentralControllerPointService.selectList(topCentralControllerPoint);
        return getDataTable(list);
    }

    /**
     * 查询简单的地图站点信息列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralControllerPoint topCentralControllerPoint){

        Collection<TopCentralControllerPoint> topCentralControllerPointList = topCentralControllerPointService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralControllerPointList,TopCentralControllerPoint::getId,TopCentralControllerPoint::getPointTitle));
    }

    /**
     * 获取地图站点信息详细信息
     */
    @RequiresPermissions("central:centralControllerPoint:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralControllerPointService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralControllerPoint 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralControllerPoint topCentralControllerPoint){
        int weigth = topCentralControllerPointService.getMaxOfWeight(topCentralControllerPoint);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载地图站点信息导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralControllerPoint> util = new ExcelUtil<>(TopCentralControllerPoint.class);
        util.importTemplateExcel(response, "地图站点信息数据");
    }

    /**
     * 导入地图站点信息数据
     * @param file 地图站点信息数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "地图站点信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralControllerPoint:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralControllerPoint> util = new ExcelUtil<>(TopCentralControllerPoint.class);
        List<TopCentralControllerPoint> topCentralControllerPointList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralControllerPointService.updateBatchById(topCentralControllerPointList)){
                return AjaxResult.success("导入地图站点信息数据覆盖成功！");
            }
            return AjaxResult.error("导入地图站点信息数据覆盖失败！");
        }
        if(topCentralControllerPointService.insertBatch(topCentralControllerPointList)){
            return AjaxResult.success("导入地图站点信息数据新增成功！");
        }
        return AjaxResult.error("导入地图站点信息数据新增失败！");
    }

    /**
     * 导出地图站点信息列表
     */
    @RequiresPermissions("central:centralControllerPoint:export")
    @Log(title = "地图站点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralControllerPoint topCentralControllerPoint)
    {
        List<TopCentralControllerPoint> list = topCentralControllerPointService.selectList(topCentralControllerPoint);
        ExcelUtil<TopCentralControllerPoint> util = new ExcelUtil<TopCentralControllerPoint>(TopCentralControllerPoint.class);
        return util.exportExcel(list, "地图站点信息数据");
    }

    /**
     * 新增地图站点信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存地图站点信息
     */
    @RequiresPermissions("central:centralControllerPoint:add")
    @Log(title = "地图站点信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralControllerPoint topCentralControllerPoint)
    {
        return toAjax(topCentralControllerPointService.insert(topCentralControllerPoint));
    }

    /**
     * 修改地图站点信息
     */
    @RequiresPermissions("central:centralControllerPoint:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralControllerPoint topCentralControllerPoint = topCentralControllerPointService.selectById(id);
        mmap.put("topCentralControllerPoint", topCentralControllerPoint);
        return prefix + "/edit";
    }

    /**
     * 修改保存地图站点信息
     */
    @RequiresPermissions("central:centralControllerPoint:edit")
    @Log(title = "地图站点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralControllerPoint topCentralControllerPoint)
    {
        return toAjax(topCentralControllerPointService.updateById(topCentralControllerPoint));
    }

    /**
     * 修改地图站点信息 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerPoint:edit")
    @Log(title = "地图站点信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralControllerPointService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 恢复地图站点信息 为未删除 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerPoint:restore")
    @Log(title = "地图站点信息", businessType = BusinessType.UPDATE)
    @PutMapping("/restoreData/{ids}")
    @ResponseBody
    public AjaxResult recoverByIds(@PathVariable("ids") List<Integer> ids){
        return toAjax(topCentralControllerPointService.recoverBatchByIds(ids));
    }

    /**
     * 删除地图站点信息
     */
    @RequiresPermissions("central:centralControllerPoint:remove")
    @Log(title = "地图站点信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralControllerPointService.logicDeleteBatchByIds(ids));
    }
}
